import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by IntelliJ IDEA.
 * User: Fatadeild Hagkaups
 * Date: 16.11.2011
 * Time: 23:37
 *
 * Þessi klasi sér um þau input sem koma frá leikmönnunum.
 */
public class Input
{
    /**
     * Meðlimabreytur
     */
    Player winner = null;
    Player player1;
    Player player2;
    protected int round;

    protected BufferedReader reader =
            new BufferedReader(new InputStreamReader(System.in));

    /**
     * Smiðir
     */
    public Input()
    {
        player1 = new Player('X');
        player2 = new Player('O');
        this.round = 1;
    }

    public Input(Player player1, Player player2)
    {
        this.player1 = player1;
        this.player2 = player2;
        this.round = 1;
    }
    /*
     * Upphafsstillir rounds;
     */
    public void newGame()
    {
        round = 0;
    }

    /**
     * Fall sem sér um að biðja báða leikmennina um nafn.
     * @throws IOException
     */
    public void askForName() throws IOException
    {
        System.out.print("Please enter name for player 1: ");
        player1.setPlayerName(reader.readLine());

        System.out.print("Please enter name for player 2: ");
        player2.setPlayerName(reader.readLine());
    }

    /**
     * Breytir á milli leikmanna eftir hvaða round er.
     * Oddatölu rounds = player1, jafntölu rounds = player2
     * @return Þeim leikmanni sem er komið að.
     */
    public Player checkTurn()
    {
        if(this.getRound() % 2 != 0)
            return player1;
        else
            return player2;
    }

    /**
     * Fær input frá lyklaborði.
     * Athugar hvort að þetta sé valid input.
     * @return Tölu á milli 1-9
     * @throws IOException
     */
    public int getInputFromConsole() throws IOException
    {
        System.out.print(" Input MUST be number between 1 - 9: ");
        String inputFromConsole = reader.readLine();
        char input = ' ';
        if(inputFromConsole.length() != 0)
            input = inputFromConsole.charAt(0);
        if(!Character.isLetter(input) && Character.isDigit(input) && inputFromConsole.length() == 1 && inputFromConsole.length() != 0 && !inputFromConsole.equals("0"))
        {
            int intInputFromConsole = Integer.parseInt(inputFromConsole);
            return intInputFromConsole;
        }
        else
        {
            System.out.print(" Please DO as you are told! ");
            return getInputFromConsole();
        }
    }

    /**
     * Fall sem skilar númer hvað þessi round er.
     * @return Númer hvað þessi round er.
     */
    public int getRound()
    {
        return round;
    }

    public void setRound(int round) {
        this.round = round;
    }

    /**
     * Eykur round.
     */
    public void incRound()
    {
        this.round++;
    }

    /**
     * Fall sem skilar hver vann leikinn.
     * @return Winner
     */
    public Player getWinner()
    {
        return winner;
    }

    /**
     * Fall sem setur hver er winner.
     * @param winner Winner leiksins.
     */
    public void setWinner(Player winner)
    {
        this.winner = winner;
    }

    /**
     * Athugar hvort að þeir vilji spila annan leik.
     * @return - skilar true ef þeir vilja spila aftur, annars false
     * @throws IOException
     */
    public boolean playAgain() throws IOException {

        if(winner == player1)
        {
            System.out.println(player1.toString());
            System.out.println(player2.toString());
        }
        else
        {
            System.out.println(player2.toString());
            System.out.println(player1.toString());
        }
        System.out.print("\nDo you want to play another game? (Y/N) ");

        String answerFromConsole = reader.readLine();

        if(answerFromConsole.length()== 1 && !answerFromConsole.equals("") && !answerFromConsole.equals("0"))
        {
            char charAnswerFromConsole = answerFromConsole.charAt(0);

            if(charAnswerFromConsole == 'Y' || charAnswerFromConsole == 'y')
                return true;
        }

        return false;
    }
}



